﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using MyATM.Entity;

namespace MyATM.DataAccess
{
    /// <summary>
    /// This class provide methods to connect and process with the database.
    /// </summary>
    public class SqlAccess
    {
        //Khai bao chuoi ket noi
        public static OleDbConnection oleDbConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory.ToString() + "ATM.accdb");

        #region ExcuteQuery
        /// <summary>
        /// ExcuteQuery
        /// </summary>
        /// <param name="sqlQuery">Cau truy van SQL</param>
        /// <returns>Bang du lieu</returns>
        public static DataTable ExcuteQuery(string sqlQuery)
        {
            DataSet ds = new DataSet();
            try
            {
                oleDbConnection.Open();
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = new OleDbCommand(sqlQuery, oleDbConnection);
                da.Fill(ds);

            }
            catch (SqlException exception)
            {
                
                throw new Exception(exception.Message);
            }
            finally
            {
                oleDbConnection.Close();
            }
            return ds.Tables[0];
        }
        #endregion

        #region ExcuteNonQuery
        /// <summary>
        /// ExcuteNonQuery
        /// </summary>
        /// <param name="sqlQuery"></param>
        /// <returns></returns>
        public static int ExcuteNonQuery(string sqlQuery)
        {
            int result;
            try
            {
                oleDbConnection.Open();
                OleDbCommand oleDbCommand = oleDbConnection.CreateCommand();
                oleDbCommand.CommandText = sqlQuery;
                oleDbCommand.CommandType = CommandType.Text;
                result = oleDbCommand.ExecuteNonQuery();

            }
            catch (SqlException exception)
            {
                throw new Exception(exception.Message);
            }
            finally
            {
                oleDbConnection.Close();
            }
            return result;
        }
        #endregion
    }
}
